﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web.Script.Serialization;

namespace YbxLibrary
{
    public class Yb
    {
        /// <summary>
        /// 医保交易
        /// </summary>
        /// <param name="infno">交易编号</param>
        /// <param name="jysr">交易输入</param>
        /// <returns></returns>
        public static string YhHbCall(string infno,string jysr)
        {
            string backStr = string.Empty;
            try
            {
                #region 入参初始化
                var yhType = Type.GetTypeFromProgID("yinhai.yh_hb_sctr");
                var yhObject = System.Activator.CreateInstance(yhType);
                var parameterModifier = new ParameterModifier(3);
                parameterModifier[0] = false;
                parameterModifier[1] = false;
                parameterModifier[2] = true;
                var parameterModifiers = new ParameterModifier[] { parameterModifier };
                #endregion

                #region 调用
                var args = new object[] { infno, jysr, backStr };
                yhType.InvokeMember("yh_hb_call", BindingFlags.InvokeMethod, null, yhObject, args, parameterModifiers, System.Globalization.CultureInfo.CurrentCulture, null);
                backStr = args[2].ToString();
                #endregion

                #region 资源释放
                try
                {
                    parameterModifiers = null;
                    yhObject = null;
                    yhType = null;
                }catch {}
                #endregion

            }catch {
                backStr = string.Empty;            
            }
            return backStr;
        }

        /// <summary>
        /// 直接调用
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static YbEntity YhHbCall(ref YbEntity entity)
        {
            entity.input_json = Json.BuildJsonString(entity.infno, entity.input, entity.opter, entity.opter_name, entity.mdtrtarea_admdvs, entity.insuplc_admdvs, entity.sign_no);
            entity.output_json = string.Empty;
            entity.infcode = -1;
            entity.err_msg = string.Empty;

            try
            {
                #region 入参初始化
                var yhType = Type.GetTypeFromProgID("yinhai.yh_hb_sctr");
                var yhObject = System.Activator.CreateInstance(yhType);
                var parameterModifier = new ParameterModifier(3);
                parameterModifier[0] = false;
                parameterModifier[1] = false;
                parameterModifier[2] = true;
                var parameterModifiers = new ParameterModifier[] { parameterModifier };
                #endregion

                #region 调用
                var args = new object[] { entity.infno, entity.input_json, entity.output_json };
                yhType.InvokeMember("yh_hb_call", BindingFlags.InvokeMethod, null, yhObject, args, parameterModifiers, System.Globalization.CultureInfo.CurrentCulture, null);
                entity.output_json = args[2].ToString();
                #endregion

                #region 资源释放
                try
                {
                    parameterModifiers = null;
                    yhObject = null;
                    yhType = null;
                }
                catch {}
                #endregion

                #region  解析出参
                try
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    serializer.MaxJsonLength = int.MaxValue;
                    Dictionary<string, object> rootObject = serializer.Deserialize<Dictionary<string, object>>(entity.output_json);

                    entity.infcode = Convert.ToInt32(rootObject["infcode"]);
                    entity.err_msg = Convert.ToString(rootObject["err_msg"]);
                    var outputObject = rootObject["output"] as Dictionary<string, object>;
                    entity.output = Convert.ToString(serializer.Serialize(outputObject));
                } catch {}
                #endregion
            }
            catch (Exception ex)
            {
                entity.infcode = -2;
                entity.err_msg = "交易调用出错" + ex.Message;
                entity.output_json = string.Empty;
            }

            return entity;
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static YbEntity YhHbCallLite(ref YbEntity entity)
        {
            entity.input_json = Json.BuildJsonString(entity.infno, entity.input, entity.opter, entity.opter_name, entity.mdtrtarea_admdvs, entity.insuplc_admdvs, entity.sign_no);
            entity.output_json = string.Empty;
            entity.infcode = -1;
            entity.err_msg = string.Empty;

            try
            {

                #region 入参初始化
                var yhType = Type.GetTypeFromProgID("yinhai.yh_hb_sctr");
                var yhObject = System.Activator.CreateInstance(yhType);
                var parameterModifier = new ParameterModifier(3);
                parameterModifier[0] = false;
                parameterModifier[1] = false;
                parameterModifier[2] = true;
                var parameterModifiers = new ParameterModifier[] { parameterModifier };
                #endregion

                #region 调用
                var args = new object[] { entity.infno, entity.input_json, entity.output_json };
                yhType.InvokeMember("yh_hb_call", BindingFlags.InvokeMethod, null, yhObject, args, parameterModifiers, System.Globalization.CultureInfo.CurrentCulture, null);
                entity.output_json = args[2].ToString();
                #endregion

                #region 资源释放
                try
                {
                    parameterModifiers = null;
                    yhObject = null;
                    yhType = null;
                }
                catch {}
                #endregion

                #region  解析出参
                try
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    serializer.MaxJsonLength = int.MaxValue;
                    Dictionary<string, object> rootObject = serializer.Deserialize<Dictionary<string, object>>(entity.output_json);

                    entity.infcode = Convert.ToInt32(rootObject["infcode"]);
                    entity.err_msg = Convert.ToString(rootObject["err_msg"]);
                    var outputObject = rootObject["output"] as Dictionary<string, object>;
                    entity.output = Convert.ToString(serializer.Serialize(outputObject));
                }
                catch{ }
                #endregion

                #region 记录日志
                Logger.Log("入参:" + entity.input_json);
                Logger.Log("出参:" + entity.output_json);
                #endregion

            }
            catch (Exception ex)
            {
                entity.infcode = -2;
                entity.err_msg = "交易调用出错" + ex.Message;
                entity.output_json = string.Empty;
            }
            return entity;
        }
    }
}
